import * as tools from './tools.js';
import '../sass/common.scss';
import {initHeaderFooter} from './gopLib/initHeaderFooter';
jQuery.support.cors = true;
hljs.configure({
    tabReplace: ' ', // 4 spaces
    classPrefix: ''     // don't append class prefix
    // … other options aren't changed
})
hljs.initHighlightingOnLoad();
let ALL_ = {};
let ALL_ARR = [];
let ID = tools.getArg('id');
let PID = -1;
let NID = -1;

$(function () {
    initHeaderFooter();
    $('.help_').addClass('active');
    initPlatForm();
   
    $('.p_btn').click(function () {
        if ($(this).hasClass('disabled')) return
        window.location.href = './platInfoDetail.html?id=' + PID;
    })
    
    $('.n_btn').click(function () {
        if ($(this).hasClass('disabled')) return
        window.location.href = './platInfoDetail.html?id=' + NID;
    })
})

function initPlatForm() {
    $.ajax({
        url: tools.apiUrl + 'rolledImage/imgList',
        type: 'post',
        contentType: 'text/xml',
        data: 'PLATFORM',
        success: function (res) {
            let data = res.data;
            ALL_ARR = data;
            if (res.code == '200') {
                forMartaData(data);
                initData(ALL_ARR[ALL_[ID].index]);
                initBTN(ALL_[ID].index);
            }
        }
    });
}

function forMartaData(data) {
    data.forEach((val, index) => {
        ALL_[val.id] = {
            index,
        }
    })
}

function initData(data) {
    let headHtml = `
        <h1>${data.name}</h1>
        <p class="bs-info"><span class="t-z">2018-5-18</span><span>来源:<a href="">安迅物流</a></span></p>
     
    `
    let html = `
        <div class="main_content">${data.content}</div>
        <div style="text-align:center;margin-top:-70px">
            <img width="100%" src="${data.url}" />
        </div>
        <div class="dir"></div>
    `;
    let htmlFoot = `

        <div class="end-line">
        <span>END</span>
    </div>
    `
    $('.platInfo_detail_box_head').html(headHtml);
    $('.platInfo_detail_box_content').html(html);
    $('.platInfo_footer').html(htmlFoot);
}

function initBTN(index) {
    let maxIndex = ALL_ARR.length - 1;

    //是否是最后一个
    if (index == maxIndex) {
        $('.p_btn').removeClass('disabled');
        NID = -1;
        PID = ALL_ARR[index - 1].id;
    } else {
        if (index == 0) {
            $('.n_btn').removeClass('disabled');
            PID = -1;
            NID = ALL_ARR[index + 1].id;

        } else {
            $('.p_btn').removeClass('disabled');
            $('.n_btn').removeClass('disabled');
            PID = ALL_ARR[index - 1].id;
            NID = ALL_ARR[index + 1].id;
        }
    }
}

